//==================================================================================
//		Dual Quad Board Program
//		file name : Key_Control_Function.C
//		version : 1.0
//		target device : w78e516(winbond)
//		target board : dual quad board
//		c compiler : Keil
//==================================================================================

void Channel_Freeze_Function_ON_OFF();
void Channel_Full_Display_Function_Initial();
void Auto_Sequence_Function_ON_OFF();
void Zoom_Function_ON_OFF();

void Channel_Freeze(U8 ch_num, U8 on_off);
//void Zoom_test(U8 zoom_x_pos,U8 zoom_y_pos, U8 on_off);

void Channel_Freeze_Function();
void Mode_Change_Function();

void Display_PIP();
void Display_PIP1_Function();
void Display_PIP2_Function();
U8 	PIP_Channel_Select_PIP_2(U8 pip_sel);
void Pseudo_16ch_Function();	
void Display_Quad_sel() ;
//void Display_Realtime8_sel();
void Display_Splite_2();
void Display_Splite_2_Function();
void Channel_Control_Splite_2_Move(U8, U8, U8);
void Splite_Auto_Move_Function();
//void Splite_Move_OSD_Display();

void Auto_Sequence_Function();
void Zoom_Function();
void Border_Line_Setup_Initial();
void Camera_Setup_Initial();
//void Motion_Setup_Initial();

void Event_Reset();

//void Led_On_Off_Display();
//void Check_Path_Set(U8 pip_sel);
void Pip_Osd_Display(U8 pos_sel, U8 property, U8 display_clear, U8 read_add);
void Realtime8_Display_sel(U8 pos_param, U8 property);
void Channel_Pos_Setup(U8 ch_num, U8 pic_hl, U8 pic_vt, U8 pic_hr, U8 pic_vb);
void Quad_Ch_Pos_Setup(U8 ch_num);
void Pip_Channel_Pos_Setup(U8 ch_num, U8 ch_pos);
void Register_Setup(U8 ch_num, U8 mask_bit, U8 set_bit, U8 off_set);
void Display_Quad_Function(U8 ch1, U8 ch2, U8 ch3, U8 ch4);
void Quad_Display_Function();
void RealTime_8Ch_Display_Function();

//void RS232_TX_Data_Setup();
extern void Loss_Channel_Read(U8 chip_sel, U8 start_chip, U8 end_chip, U8 start_ch, U8 end_ch);	
extern void Motion_Channel_Read(U8 chip_sel, U8 start_chip, U8 end_chip, U8 start_ch, U8 end_ch);	

#if 0
//==================================================================================
//
//
//
//
//
//==================================================================================
void ch_control_off(void)
{
	ch_control_on_off = OFF;
}
#endif
//==================================================================================
//
//
//
//
//
//==================================================================================
void ch_control_on(void)
{
	ch_control_on_off = ON;
}

//==================================================================================
//
//
//
//
//
//==================================================================================
void Realtime8_Display_sel(U8 pos_param, U8 property)
{
	U8 temp, N_P = 0;

	temp = ReadAsicByte((pos_param <5) ? MASTER : SLAVE1, DVC_PG0, 0x0d + 0x10 *((pos_param -1) %4));
	if((temp &0x08) != 0)
		if(pos_param < 5)
			temp = pos_param +3;
		else
			temp = pos_param +7;
	else					
		if(pos_param < 5)
			temp = pos_param-1;
		else
			temp = pos_param +3;
					
	switch(pos_param)
	{
		case 1 :
		case 2 :
		case 3 :
				if(b_video_fmt == NTSC)
					N_P = 16;
				Write_osd(11 + (pos_param -1) *30, 84-N_P, property, 0xc0 +temp *4);
				break;
		case 4 :
		case 5 :
				if(b_video_fmt == NTSC)
					N_P = 32;
				Write_osd(11 + (pos_param -4) *60, 180-N_P, property, 0xc0 +temp *4);
				break;
		case 6 :
		case 7 :
		case 8 :	
				if(b_video_fmt == NTSC)
					N_P = 40;
				Write_osd(11 + (pos_param -6) *30, 258 -N_P, property, 0xc0 +temp *4);
				break;							
	}
}

//==================================================================================
//
//
//
//
//
//==================================================================================
void Pip_Channel_Pos_Setup(U8 ch_num, U8 ch_pos)
{
	switch(ch_pos)
		{
			case 1 :
					if(b_video_fmt == NTSC)
						Channel_Pos_Setup(ch_num, 0x64, 0x3d, 0xa0, 0x64);
					else
						Channel_Pos_Setup(ch_num, 0x64, 0x4c, 0xa0, 0x7c);
					break;
			case 2 :
					if(b_video_fmt == NTSC)
						Channel_Pos_Setup(ch_num, 0x14, 0x3d, 0x50, 0x64);
					else
						Channel_Pos_Setup(ch_num, 0x14, 0x4c, 0x50, 0x7c);
					break;
			case 3 :
					if(b_video_fmt == NTSC)
						Channel_Pos_Setup(ch_num, 0x14, 0x0c, 0x50, 0x33);
					else
						Channel_Pos_Setup(ch_num, 0x14, 0x10, 0x50, 0x40);
					break;
			case 4 :
					if(b_video_fmt == NTSC)
						Channel_Pos_Setup(ch_num, 0x64, 0x0c, 0xa0, 0x33);
					else
						Channel_Pos_Setup(ch_num, 0x64, 0x10, 0xa0, 0x40);
					break;
		}
}

//==================================================================================
//
//
//
//
//
//==================================================================================
void Register_Setup(U8 ch_num, U8 mask_bit, U8 set_bit, U8 off_set)
{
	U8 temp, m_s;

	if(ch_num < 4)
		m_s= MASTER;
	else
		m_s= SLAVE1;
	
	temp = ReadAsicByte(m_s, DVC_PG1, (0x10 +(ch_num %4) *8 +off_set));
	temp &= mask_bit;
	temp |= set_bit;
	WriteAsicByte(m_s, DVC_PG1, (0x10 +(ch_num %4) *8 +off_set), temp);
}

//==================================================================================
//
//
//
//
//
//==================================================================================
void Channel_Pos_Setup(U8 ch_num, U8 pic_hl, U8 pic_vt, U8 pic_hr, U8 pic_vb)
{
	U8 m_s;

	if(ch_num < 4)
		m_s = MASTER;
	else
		m_s = SLAVE1;

	WriteAsicByte(m_s, DVC_PG1, (0x30+(ch_num % 4) * 0x04), pic_hl);	
	WriteAsicByte(m_s, DVC_PG1, (0x31+(ch_num % 4) * 0x04), pic_hr);	
	WriteAsicByte(m_s, DVC_PG1, (0x32+(ch_num % 4) * 0x04), pic_vt);	
	WriteAsicByte(m_s, DVC_PG1, (0x33+(ch_num % 4) * 0x04), pic_vb);
}

//==================================================================================
//
//param :
//	pos_sel	:
//	property  :
//   display_clear : 	1 : display  osd  0: clear od
//==================================================================================
void Pip_Osd_Display(U8 pos_sel, U8 property, U8 display_clear, U8 read_add)
{
	U8 N_P = 0;

	if(b_video_fmt == NTSC)
		N_P = 48;
	switch(pos_sel)
	{
		case 1 :
				if(display_clear == 1)
					Write_osd(62, 237-N_P, property, read_add);
				else
					WriteOSDClr(PTH_Y, MASTER, 62, 237-N_P, 8, 12);
				break;
		case 2 :
				if(display_clear == 1)
					Write_osd(20, 237-N_P, property, read_add);
				else
					WriteOSDClr(PTH_Y, MASTER, 20, 237-N_P, 8, 12);
				break;				
		case 3 :
				if(b_video_fmt == NTSC)
					N_P = 26;
				if(display_clear == 1)
					Write_osd(20, 117-N_P, property, read_add);
				else
					WriteOSDClr(PTH_Y, MASTER, 20, 117-N_P, 8, 12);
				break;
		case 4 :
				if(b_video_fmt == NTSC)
					N_P = 26;
				if(display_clear == 1)
					Write_osd(62, 117-N_P, property, read_add);
				else
					WriteOSDClr(PTH_Y, MASTER, 62, 117-N_P, 8, 12);
				break;			;
	}
}

//==================================================================================
//
//
//
//
//
//==================================================================================
/*void Check_Path_Set(U8 pip_sel)
{
	U8 mask_bit = 0x01;
	
	channel_path_Check |= (mask_bit << pip_sel);
	if(pip_sel > 7)
		channel_path_Check &= ((mask_bit << (pip_sel - 8)) ^ 0xff);
	else
		channel_path_Check &= ((mask_bit << (pip_sel + 8)) ^ 0xff);
}
*/
//==================================================================================
//
//
//
//
//
//==================================================================================
void Channel_Freeze_Function_ON_OFF()
{
//	if(key_code ==  FREEZE_KEY)
//	{
		led_on_off_flag = key_code;
		key_sound = ON;
		Event_Reset();
		RemoveBox();
		if(auto_mode_flag ==ON)
		{
			auto_sequence_on ^=1;
			one_sec = 0;
		}
		if(display_mode == SPLITE_2)
			splite_2_auto_move ^=1;
		freeze_on ^= 1;
//		freeze_option = ON;
		Channel_Freeze_Function();
//	}
}

//==================================================================================
//
//
//
//
//
//==================================================================================
void Channel_Freeze_Function()
{
	U8 i, ch_read_data = 0;

	if(freeze_on == ON){
		ch_read_data = 0x48;
		WriteOSDStr(PTH_Y,MASTER, 462, 9, OSD_NOR, "F");
	}
	else
		WriteOSDClr(PTH_Y, MASTER, 462, 9, 2, 12);
	for(i =0; i <8; i++)
		Register_Setup(i, 0xb7, ch_read_data, 1);
}

#if 0
Zoom_Ch_test(U8 ch_num)
{
	if(zoom_on)
	
		WriteAsicByte(ALL_CHIP, DVC_PG1,0x0c,0xaf);
	else
		WriteAsicByte(ALL_CHIP, DVC_PG1,0x0c,0x2f);
	WriteAsicByte(ALL_CHIP,DVC_PG1,0x0d,POS_6CH[ch_num*2]);
	WriteAsicByte(ALL_CHIP,DVC_PG1,0x0e,POS_6CH[ch_num*2+1]+0x5a);
}
#endif
code U8 POS_8CH_F[]={
	0x12,		0x2e ,
	0x12 * 2,	0x2e ,
	0x12 * 3,	0x2e ,
	0x12,    	0x2e * 3,
	0x12 * 3,	0x2e * 3,
	0x12,    	0x2e * 5,
	0x12 * 2,	0x2e * 5,
	0x12 * 3,	0x2e * 5,
};

#if 0
void Zoom_test(U8 zoom_x_pos,U8 zoom_y_pos, U8 on_off)
{
	if(on_off)
	{
		WriteAsicByte(ALL_CHIP, DVC_PG1,0x0c,0xaf);
		if(zoom_x_pos < 0x00)
		zoom_x_pos = 0x00;
			else if(zoom_x_pos > 0x5a);
		zoom_x_pos = 0x5a;

		if(zoom_y_pos < 0x00)
			zoom_y_pos = 0x00;
		else if(zoom_y_pos > 0x48);
			zoom_y_pos = 0x48;
		WriteAsicByte(ALL_CHIP,DVC_PG1,0x0d,zoom_x_pos);
		WriteAsicByte(ALL_CHIP,DVC_PG1,0x0e,zoom_y_pos);
	}
	else
	{
		WriteAsicByte(ALL_CHIP, DVC_PG1,0x0c,0x2f);
	}

}
#endif

void Channel_Freeze(U8 ch_num, U8 on_off)
{
	U8 _dvc,ch_freeze = 0;
	if(ch_num<4)
		_dvc = MASTER;
	else
		_dvc = SLAVE1;
	
	if(on_off == ON)
	{
		Register_Setup(ch_num, 0xb7, 0x40, 1);	
		WriteOSDStr(PTH_Y,_dvc, POS_8CH_F[ch_num * 2], POS_8CH_F[ch_num * 2 + 1],OSD_NOR,"F");
	}
	else
	{
		Register_Setup(ch_num, 0xb7, 0x00, 1);	
		WriteOSDClr(PTH_Y, _dvc, POS_8CH_F[ch_num * 2], POS_8CH_F[ch_num * 2 + 1], 2, 12);
	}
	
}


//==================================================================================
//
//
//
//
//
//==================================================================================
void Quad_Display_Function()
{
	static U8 quad_abcd;
	
	led_on_off_flag = key_code;
	key_sound = ON;
	Event_Reset();
	RemoveBox();
	freeze_on = OFF;
	Channel_Freeze_Function();
	RemoveOSDFnt(0, 22, 89, 287);			//Clear OSD
	WriteOSDClr(PTH_Y, MASTER, 454, 9, 8, 12);
	auto_sequence_on = OFF;
	auto_mode_flag =OFF;
	splite_2_auto_move = OFF;
	ch_control_on_off = OFF;
	channel_select_cursor = 0;
		
	if(((display_mode !=QUAD_A)&&(display_mode !=QUAD_B))||(quad_abcd >1))
		quad_abcd = 0;
	display_mode = QUAD_A+quad_abcd*5;
	display_mode_backup = display_mode;
	pip_full_ch = quad_abcd*4;
	pip_sub_ch_1 = pip_full_ch +1;
	pip_sub_ch_2 = pip_full_ch +2;
	pip_sub_ch_3 = pip_full_ch +3;
	channel_path_Check = 0x0f;
	channel_path_Check <<=quad_abcd*4;
	quad_abcd++;
		
	Display_Quad_Function(pip_full_ch, pip_sub_ch_1, pip_sub_ch_2, pip_sub_ch_3);	
}

//==================================================================================
//
//
//
//
//
//==================================================================================
void RealTime_8Ch_Display_Function()
{	
	led_on_off_flag = key_code;
	key_sound = ON;
	Event_Reset();
	RemoveBox();
	freeze_on = OFF;
	Channel_Freeze_Function();
	RemoveOSDFnt(0, 0, 89, 287);			//Clear OSD
	auto_sequence_on = OFF;
	auto_mode_flag =OFF;
	splite_2_auto_move = OFF;
	ch_control_on_off = OFF;
//	channel_select_cursor = 0;
		
	channel_path_Check = 0xff;
	display_mode = REALTIME_8CH;
	display_mode_backup = display_mode;
	
	Display_8ch();
}

//==================================================================================
//
//
//
//
//
//==================================================================================
void Mode_Change_Function()       
{                                                               
	U8 buff;
	U8 mask_bit = 0x01;

	led_on_off_flag = key_code;
	key_sound = ON;
	Event_Reset();
	RemoveBox();
	freeze_on = OFF;
	Channel_Freeze_Function();
	RemoveOSDFnt(0, 22, 89, 287);			//Clear OSD
	WriteOSDClr(PTH_Y, MASTER, 454, 9, 8, 12);
	auto_sequence_on = OFF;
	auto_mode_flag =OFF;
	ch_control_on_off = OFF;
	splite_2_auto_move = OFF;
	channel_select_cursor = 0;

	if((display_mode != PIP1_MODE) &&(display_mode != PIP2_MODE)&&(display_mode != SPLITE_2)&&(display_mode != REALTIME_8CH_M_A))
		display_mode_change = 0;
	display_mode_change++;
	if(display_mode_change >4)
		display_mode_change = 1;		
		
	switch (display_mode_change) 
	{
		case 1 : 			
				display_mode = PIP2_MODE;		
				display_mode_backup = display_mode;
				buff = I2C_Data_Read(rom_p0, 0xfa);		//... PIP2 Channel Information Read					
				pip_full_ch = (buff & 0xf0) >> 4;
				pip_sub_ch_1 = buff & 0x0f;
				buff = I2C_Data_Read(rom_p0, 0xfb);		//... PIP2 Channel Information Read
				pip_sub_ch_2 = buff & 0x0f;	
						
				channel_path_Check = 0;
				channel_path_Check |= (mask_bit << pip_full_ch);
				channel_path_Check |= (mask_bit << pip_sub_ch_1);
				channel_path_Check |= (mask_bit << pip_sub_ch_2);
						
				Display_PIP2_Function();
				break;					
		case 2 :
				buff = I2C_Data_Read(rom_p0, 0xf9);		//... PIP1 Channel Information Read					
				pip_full_ch = (buff & 0xf0) >> 4;
				pip_sub_ch_1 = buff & 0x0f;
				channel_path_Check = 0;
				channel_path_Check |= (mask_bit << pip_full_ch);
				channel_path_Check |= (mask_bit << pip_sub_ch_1);						
				display_mode = PIP1_MODE;		
				display_mode_backup = display_mode;				
				
				Display_PIP2_Function();
				break;								
		case 3 : 
				buff = I2C_Data_Read(rom_p0, 0xfc);		//... Splite 2 Channel Information Read					
				pip_full_ch = (buff & 0xf0) >> 4;
				pip_sub_ch_1 = buff & 0x0f;
				
				channel_path_Check = 0;
				channel_path_Check |= (mask_bit << pip_full_ch);
				channel_path_Check |= (mask_bit << pip_sub_ch_1);
						
				display_mode = SPLITE_2;
				display_mode_backup = display_mode;
				splite_2_auto_move = ON;
				splite_2_flag = OFF;
				sub_menu_select_bar = 696;
				splite_2_auto_stop = OFF;
				splite_2_auto_flag_R = 1;
				splite_2_auto_flag_L = 1;
/*
				N_P	= (b_video_fmt == PAL) ? 12 : 0;			
				SetBox(2, 34, 18, 20+N_P, 15);
				SetBoxPlaneSel(2, 3, 6);
				SetBoxMix(PTH_X,2,ON);		        //... set box mix
				SetBoxBnd(PTH_X,2,ON);			 //... set box boundary
				SetBoxPln(PTH_X,2,ON);			//... set box plane area

				SetBox(3, 298, 18, 20+N_P, 15);	
				SetBoxPlaneSel(2, 3, 6);
				SetBoxMix(PTH_X,3,ON);		        //... set box mix
				SetBoxBnd(PTH_X,3,ON);			 //... set box boundary
				SetBoxPln(PTH_X,3,ON);			//... set box plane area
*/
				Display_Splite_2_Function();
				break;	
		case 4:
				display_mode = REALTIME_8CH_M_A;
				display_mode_backup = display_mode;
				channel_path_Check = 0xff;
				Display_8ch_AB();
	}	
}

//==================================================================================
//
//
//
//
//
//==================================================================================
void Channel_Full_Display_Function_Initial()
{
	U8  mask_bit = 0x01;

	if((key_code > 8) && (key_code < 17))
	{
		led_on_off_flag = key_code;
		key_sound = ON;
		display_mode = key_code -7+(key_code-9)/4;
		if(display_mode_backup == display_mode) return;
		Event_Reset();
		RemoveBox();
		freeze_on = OFF;
		Channel_Freeze_Function();
		RemoveOSDFnt(0, 22, 89, 287);			//Clear OSD
		WriteOSDClr(PTH_Y, MASTER, 454, 9, 8, 12);
		auto_sequence_on = OFF;
		auto_mode_flag =OFF;
		splite_2_auto_move = OFF;
		channel_path_Check = mask_bit <<(key_code -9);
		
		Display_Full(key_code-9);
		display_mode_backup = display_mode;
//		key_code = 0;
	}
}

//==================================================================================
//
//
//
//
//
//==================================================================================
void Display_Realtime8_sel()
{
	U8 buff, temp;
	U16 mask_bit = 0x0001;

	if((key_code == ENTER_KEY))
	{
		key_code = 0;
		key_sound = ON;
		ch_control_on_off ^= 1;
		if(ch_control_on_off == ON)
		{
			channel_select_cursor = 1;
			Realtime8_Display_sel(channel_select_cursor, OSD_BLK); 
		}
		else
		{
			channel_select_cursor = 0;
			Realtime8_Display_sel(1, OSD_NOR);
			Realtime8_Display_sel(2, OSD_NOR);
			Realtime8_Display_sel(3, OSD_NOR);
			Realtime8_Display_sel(4, OSD_NOR);
			Realtime8_Display_sel(5, OSD_NOR);
			Realtime8_Display_sel(6, OSD_NOR);
			Realtime8_Display_sel(7, OSD_NOR);
			Realtime8_Display_sel(8, OSD_NOR);
		}
	}
	
	if(ch_control_on_off == ON)
	{
		if( (key_code == CH2_KEY))
		{
			key_code = 0;
			key_sound = ON;

			Realtime8_Display_sel(channel_select_cursor, OSD_NOR);
			channel_select_cursor = (channel_select_cursor >=8) ? 1 : channel_select_cursor+1;
			Realtime8_Display_sel(channel_select_cursor, OSD_BLK); 
		}
		else if((key_code == CH1_KEY))
		{
			key_code = 0;
			key_sound = ON;

			Realtime8_Display_sel(channel_select_cursor, OSD_NOR);
			channel_select_cursor = (channel_select_cursor <= 1) ? 8 : channel_select_cursor-1;
			Realtime8_Display_sel(channel_select_cursor, OSD_BLK);
		}

		if((key_code == CH3_KEY)  || (key_code == CH4_KEY))
		{
				key_code = 0;
				key_sound = ON;

				buff = channel_select_cursor < 5 ? MASTER : SLAVE1;
				temp = ReadAsicByte(buff, DVC_PG0, 0x0d + 0x10 *((channel_select_cursor -1) %4));
                        #if 0 //Tiger Liu,
                            if((temp &0x08) != 0)
					Ana_Sw_Function(channel_select_cursor-1+ (buff == SLAVE1 ? 4 : 0));
				else
					Ana_Sw_Function(channel_select_cursor +3+(buff == SLAVE1 ? 4 : 0));
                        #endif
                            //WriteAsicByte(buff, DVC_PG0, 0x0d + 0x10 *((channel_select_cursor -1) %4), temp);
				if(channel_select_cursor < 5)
				{
					if((temp &0x08) == 0 )
					{
						Register_Setup(channel_select_cursor-1, 0x3f,0xc0, 0);
						channel_path_Check &= ~(mask_bit << (channel_select_cursor +3));
						channel_path_Check |= mask_bit << (channel_select_cursor -1);
					}	
					else
					{
						Register_Setup(channel_select_cursor -1, 0x3f,0x80, 0);
						Register_Setup(channel_select_cursor +3, 0x3f,0xc0, 0);
						channel_path_Check &= ~(mask_bit << (channel_select_cursor -1));
						channel_path_Check |= mask_bit << (channel_select_cursor +3);
					}
				}
				else
				{
					if((temp &0x08) == 0 )
					{
						Register_Setup(channel_select_cursor +3, 0x3f, 0xc0, 0);
						channel_path_Check &= ~(mask_bit << (channel_select_cursor +7));
						channel_path_Check |= mask_bit << (channel_select_cursor +3);
					}
					else
					{
						Register_Setup(channel_select_cursor +3, 0x3f, 0x80, 0);
						Register_Setup(channel_select_cursor +7, 0x3f, 0xc0, 0);
						channel_path_Check &= ~(mask_bit << (channel_select_cursor +3));
						channel_path_Check |= mask_bit << (channel_select_cursor +7);
					}
				}

					
//				buff = I2C_Data_Read(rom_p0, 0xfd);
//				buff |= pip_full_ch << 4;
//				buff |= pip_sub_ch_1;
//				I2C_Data_Write(rom_p0, 0xfd, buff);
		
				Realtime8_Display_sel(channel_select_cursor, OSD_BLK);
		}
	}
}

//==================================================================================
//
//
//
//
//
//==================================================================================
void Display_Quad_sel()
{
	U8 i;
	U8 mask_bit = 0x01;

	if(key_code == ENTER_KEY)
	{
		ch_control_on_off ^= 1;
		if(ch_control_on_off == ON)
			channel_select_cursor = 1;
		else
			channel_select_cursor = 0;			
		PIP_Title_display(); 
	}
	
	if(ch_control_on_off == ON)
	{
		if(key_code == DOWN_KEY)
		{
			channel_select_cursor = (channel_select_cursor >= 4) ? 1 : channel_select_cursor+1;
			PIP_Title_display();			
		}
		else if(key_code == UP_KEY)
		{
			channel_select_cursor = (channel_select_cursor <= 1) ? 4 : channel_select_cursor-1;
			PIP_Title_display();
		}

		if(channel_select_cursor == 1)
		{
			if((key_code == LEFT_KEY)||(key_code == RIGHT_KEY))
			{
				Register_Setup(pip_full_ch, 0x3f, 0x00, 0);
				channel_path_Check &= ~(mask_bit <<pip_full_ch);
				if(key_code == LEFT_KEY)
					pip_full_ch = (pip_full_ch <= 0) ? 7 : pip_full_ch -1;
				else
					pip_full_ch = (pip_full_ch >= 7) ? 0  : pip_full_ch +1;

				for(i = 0; i<3; i++)
				{
					if(pip_full_ch== pip_sub_ch_1)
						pip_full_ch = PIP_Channel_Select_PIP_2(pip_full_ch);
					if(pip_full_ch== pip_sub_ch_2)
						pip_full_ch = PIP_Channel_Select_PIP_2(pip_full_ch);
					if(pip_full_ch== pip_sub_ch_3)
						pip_full_ch = PIP_Channel_Select_PIP_2(pip_full_ch);
				}	

//				buff = I2C_Data_Read(rom_p0, 0xfd);
//				buff &= 0x0f;
//				buff |= pip_full_ch << 4;
//				I2C_Data_Write(rom_p0, 0xfd, buff);

				//Check_Path_Set(pip_full_ch);
				channel_path_Check |= (mask_bit <<pip_full_ch);
				Display_Quad(pip_full_ch, 1);
//				buff = I2C_Data_Read(rom_p0, 0x03);		//... Display Setup "Title"			1: ON  0: OFF
				if((title_display== ON) && (vcr_mode_in == OFF))
					Camera_Title_Display(); 
//				PIP_Title_display();
			}
		}
		else if(channel_select_cursor == 2)
		{
			if((key_code == LEFT_KEY)||(key_code == RIGHT_KEY))
			{	
				Register_Setup(pip_sub_ch_1, 0x3f, 0x00, 0);
				channel_path_Check &= ~(mask_bit <<pip_sub_ch_1);
				if(key_code == LEFT_KEY)
					pip_sub_ch_1 = (pip_sub_ch_1 <= 0) ? 7 : pip_sub_ch_1 -1;
				else
					pip_sub_ch_1 = (pip_sub_ch_1 >= 7) ? 0 : pip_sub_ch_1 +1;

				for(i = 0; i<3; i++)
				{
					if(pip_sub_ch_1== pip_sub_ch_2)
						pip_sub_ch_1 = PIP_Channel_Select_PIP_2(pip_sub_ch_1);
					if(pip_sub_ch_1== pip_sub_ch_3)
						pip_sub_ch_1 = PIP_Channel_Select_PIP_2(pip_sub_ch_1);
					if(pip_sub_ch_1== pip_full_ch)
						pip_sub_ch_1 = PIP_Channel_Select_PIP_2(pip_sub_ch_1);
				}
				
//				buff = I2C_Data_Read(rom_p0, 0xfd);
//				buff &= 0xf0;
//				buff |= pip_sub_ch_1;
//				I2C_Data_Write(rom_p0, 0xfd, buff);

				//Check_Path_Set(pip_sub_ch_1);
				channel_path_Check |= (mask_bit <<pip_sub_ch_1);
				Display_Quad(pip_sub_ch_1, 2);
//				buff = I2C_Data_Read(rom_p0, 0x03);		//... Display Setup "Title"			1: ON  0: OFF
				if((title_display== ON) && (vcr_mode_in == OFF))
					Camera_Title_Display(); 
//				PIP_Title_display();
			}
		}
		else if(channel_select_cursor == 3)
		{
			if((key_code == LEFT_KEY)||(key_code == RIGHT_KEY))
			{		
				Register_Setup(pip_sub_ch_2, 0x3f, 0x00, 0);
				channel_path_Check &= ~(mask_bit <<pip_sub_ch_2);
				if(key_code == LEFT_KEY)
					pip_sub_ch_2 = (pip_sub_ch_2 <= 0) ? 7 : pip_sub_ch_2-1;
				else
					pip_sub_ch_2 = (pip_sub_ch_2 >= 7) ? 0 : pip_sub_ch_2+1;				

				for(i = 0; i<3; i++)
				{
					if(pip_sub_ch_2== pip_sub_ch_3)
						pip_sub_ch_2 = PIP_Channel_Select_PIP_2(pip_sub_ch_2);
					if(pip_sub_ch_2== pip_full_ch)
						pip_sub_ch_2 = PIP_Channel_Select_PIP_2(pip_sub_ch_2);
					if(pip_sub_ch_2== pip_sub_ch_1)
						pip_sub_ch_2 = PIP_Channel_Select_PIP_2(pip_sub_ch_2);
				}
//				buff = I2C_Data_Read(rom_p0, 0xfe);
//				buff &= 0x0f;
//				buff |= pip_sub_ch_2 <<4;
//				I2C_Data_Write(rom_p0, 0xfe, buff);				

				//Check_Path_Set(pip_sub_ch_2);
				channel_path_Check |= (mask_bit <<pip_sub_ch_2);
				Display_Quad(pip_sub_ch_2, 3);
//				buff = I2C_Data_Read(rom_p0, 0x03);		//... Display Setup "Title"			1: ON  0: OFF
				if((title_display== ON) && (vcr_mode_in == OFF))
					Camera_Title_Display(); 
//				PIP_Title_display();
			}
		}
		else if(channel_select_cursor == 4)
		{
			if((key_code == LEFT_KEY)||(key_code == RIGHT_KEY))
			{			
				Register_Setup(pip_sub_ch_3, 0x3f, 0x00, 0);
				channel_path_Check &= ~(mask_bit <<pip_sub_ch_3);
				if(key_code == LEFT_KEY)
					pip_sub_ch_3 = (pip_sub_ch_3<=0) ? 7 : pip_sub_ch_3-1;
				else
					pip_sub_ch_3 = (pip_sub_ch_3>=7) ? 0 : pip_sub_ch_3+1;				
				
				for(i = 0; i<3; i++)
				{
					if(pip_sub_ch_3== pip_full_ch)
						pip_sub_ch_3 = PIP_Channel_Select_PIP_2(pip_sub_ch_3);
					if(pip_sub_ch_3== pip_sub_ch_1)
						pip_sub_ch_3 = PIP_Channel_Select_PIP_2(pip_sub_ch_3);
					if(pip_sub_ch_3== pip_sub_ch_2)
						pip_sub_ch_3 = PIP_Channel_Select_PIP_2(pip_sub_ch_3);
				}
				
//				buff = I2C_Data_Read(rom_p0, 0xfe);	//... PIP 1 Channel Information [Full : Small]
//				buff &= 0xf0;
//				buff |= pip_sub_ch_3;
//				I2C_Data_Write(rom_p0, 0xfe, buff);	//... PIP 1 Channel Information [Full : Small]

				//Check_Path_Set(pip_sub_ch_3);
				channel_path_Check |= (mask_bit <<pip_sub_ch_3);
				Display_Quad(pip_sub_ch_3, 4);	
//				buff = I2C_Data_Read(rom_p0, 0x03);		//... Display Setup "Title"			1: ON  0: OFF
				if((title_display== ON) && (vcr_mode_in == OFF))
					Camera_Title_Display(); 
//				PIP_Title_display();
			}
		}
	}
	key_code = 0;
	key_sound = ON;
}

//===================================================================================
//
//
//
//
//
//===================================================================================
//... PIP Display
void Display_PIP()         
{
	U8 buff, temp, i;
	U8 mask_bit = 0x01;

	if(key_code == ENTER_KEY)
	{
		ch_control_on_off ^= 1;
		if(ch_control_on_off == ON)
			channel_select_cursor = 1;
		else
			channel_select_cursor = 0;
		PIP_Title_display();                         
	}

	if((display_mode == PIP1_MODE) && (ch_control_on_off == ON))
	{
		if(key_code == DOWN_KEY)
		{
			channel_select_cursor = (channel_select_cursor >=3 ) ? 1 : channel_select_cursor+1;
			PIP_Title_display();			 
		}
		else if(key_code == UP_KEY)
		{
			channel_select_cursor = (channel_select_cursor <=1) ? 3 : channel_select_cursor-1;
			PIP_Title_display();
		}

		if(channel_select_cursor == 1)
		{
			if((key_code == LEFT_KEY) || (key_code == RIGHT_KEY))
			{
				Register_Setup(pip_full_ch, 0x3f, 0x00, 0);
				channel_path_Check &= ~(mask_bit <<pip_full_ch);
				if(key_code == LEFT_KEY)
					pip_full_ch = (pip_full_ch <= 0) ? 7 : pip_full_ch-1;
				else
					pip_full_ch = (pip_full_ch >= 7) ? 0 : pip_full_ch+1;

				if(pip_full_ch == pip_sub_ch_1)
					pip_full_ch = PIP_Channel_Select_PIP_2(pip_full_ch);
				
				buff = I2C_Data_Read(rom_p0, 0xf9);	//... PIP 1 Channel Information [Full : Small]
				buff &= 0x0f;
				buff |= pip_full_ch << 4;
				I2C_Data_Write(rom_p0, 0xf9, buff);	//... PIP 1 Channel Information [Full : Small]

//				Check_Path_Set(pip_full_ch);
				channel_path_Check |= (mask_bit <<pip_full_ch);
				Display_Full(pip_full_ch);	
				Register_Setup(pip_sub_ch_1, 0x3f, 0xc0, 0);
				PIP_Title_display();
			}
		}
		else if(channel_select_cursor == 2)
		{
			if((key_code == LEFT_KEY) || (key_code == RIGHT_KEY))
			{		
				Register_Setup(pip_sub_ch_1, 0x3f, 0x00, 0);
				channel_path_Check &= ~(mask_bit <<pip_sub_ch_1);
				if(key_code == LEFT_KEY)
					pip_sub_ch_1 = (pip_sub_ch_1<=0) ? 7 : pip_sub_ch_1-1;
				else
					pip_sub_ch_1 = (pip_sub_ch_1>=7) ? 0 : pip_sub_ch_1+1;
				
				
				if(pip_sub_ch_1== pip_full_ch)
					pip_sub_ch_1 = PIP_Channel_Select_PIP_2(pip_sub_ch_1);

				buff = I2C_Data_Read(rom_p0, 0xf9);	//... PIP 1 Channel Information [Full : Small]
				buff &= 0xf0;
				buff |= pip_sub_ch_1;

				I2C_Data_Write(rom_p0, 0xf9, buff);	//... PIP 1 Channel Information [Full : Small]

				//Check_Path_Set(pip_sub_ch_1);
				channel_path_Check |= (mask_bit <<pip_sub_ch_1);
				Display_PIP_Initial(pip_sub_ch_1);
				PIP_Title_display();
			}
		}
		else if(channel_select_cursor == 3)
		{
			if((key_code == LEFT_KEY) ||(key_code == RIGHT_KEY))
			{			
				temp = I2C_Data_Read(rom_p0, 0x07) &0x0f;		//... Display Setup "PIP1 Mode"		1: Right Bottom  2: Left Bottom 3: Left Top 4: Right Top
				Pip_Osd_Display(temp, OSD_NOR, 0, 0);
				
				if(key_code == LEFT_KEY)
					temp = (temp<=1) ? 4 : temp-1;
				else
					temp = (temp>=4) ? 1 : temp+1;

				I2C_Data_Write(rom_p0, 0x07,temp);
								
				Pip_Channel_Pos_Setup(pip_sub_ch_1, temp);
				PIP_Title_display();
			}
		}
	}
	else if((display_mode == PIP2_MODE) && (ch_control_on_off == ON))
	{
		if(key_code == DOWN_KEY)
		{
			channel_select_cursor = (channel_select_cursor>=5) ? 1 : channel_select_cursor+1;
			PIP_Title_display();
			
		}else if(key_code == UP_KEY)
		{
			channel_select_cursor = (channel_select_cursor<=1) ? 5 : channel_select_cursor-1;
			PIP_Title_display();
		}

		if(channel_select_cursor == 1)
		{
			if((key_code == LEFT_KEY)||(key_code == RIGHT_KEY))
			{
				Register_Setup(pip_full_ch, 0x3f, 0x00, 0);
				channel_path_Check &= ~(mask_bit <<pip_full_ch);
				if(key_code == LEFT_KEY)
					pip_full_ch = (pip_full_ch <= 0) ? 7 : pip_full_ch -1;
				else
					pip_full_ch = (pip_full_ch >= 7) ? 0 : pip_full_ch +1;
			
				for(i = 0; i<2; i++)
				{
					if(pip_full_ch == pip_sub_ch_1)
						pip_full_ch = PIP_Channel_Select_PIP_2(pip_full_ch);
					if(pip_full_ch == pip_sub_ch_2)
						pip_full_ch = PIP_Channel_Select_PIP_2(pip_full_ch);	
				}	

				buff = I2C_Data_Read(rom_p0, 0xfa);	//... PIP 1 Channel Information [Full : Small]
				buff &= 0x0f;
				buff |= pip_full_ch << 4;
				I2C_Data_Write(rom_p0, 0xfa, buff);	//... PIP 2 Channel Information [Full : Small]

				//Check_Path_Set(pip_full_ch);
				channel_path_Check |= (mask_bit <<pip_full_ch);
				Display_Full(pip_full_ch);
				Register_Setup(pip_sub_ch_1, 0x3f, 0xc0, 0);
				Register_Setup(pip_sub_ch_2, 0x3f, 0xc0, 0);
				PIP_Title_display();
			}
		}
		else if(channel_select_cursor == 2)
		{
			if((key_code == LEFT_KEY) || (key_code == RIGHT_KEY))
			{		
				Register_Setup(pip_sub_ch_1, 0x3f, 0x00, 0);
				channel_path_Check &= ~(mask_bit <<pip_sub_ch_1);
				if(key_code == LEFT_KEY)
					pip_sub_ch_1 = (pip_sub_ch_1<=0) ? 7 : pip_sub_ch_1-1;
				else
					pip_sub_ch_1 = (pip_sub_ch_1>=7) ? 0 : pip_sub_ch_1+1;						
				
				for(i = 0; i<2; i++)
				{
					if(pip_sub_ch_1== pip_sub_ch_2)
						pip_sub_ch_1 = PIP_Channel_Select_PIP_2(pip_sub_ch_1);
					if(pip_sub_ch_1== pip_full_ch)
						pip_sub_ch_1 = PIP_Channel_Select_PIP_2(pip_sub_ch_1);					
				}

				buff = I2C_Data_Read(rom_p0, 0xfa);	//... PIP 1 Channel Information [Full : Small]
				buff &= 0xf0;
				buff |= pip_sub_ch_1;

				I2C_Data_Write(rom_p0, 0xfa, buff);	//... PIP 1 Channel Information [Full : Small]

				//Check_Path_Set(pip_sub_ch_1);
				channel_path_Check |= (mask_bit <<pip_sub_ch_1);
				Display_PIP_Initial(pip_sub_ch_1);
				PIP_Title_display();
			}
		}
		else if(channel_select_cursor == 3)
		{
			if((key_code == LEFT_KEY)||(key_code == RIGHT_KEY))
			{			
				Register_Setup(pip_sub_ch_2, 0x3f, 0x00, 0);
				channel_path_Check &= ~(mask_bit <<pip_sub_ch_2);
				if(key_code == LEFT_KEY)
					pip_sub_ch_2 = (pip_sub_ch_2<=0) ? 7: pip_sub_ch_2-1;
				else
					pip_sub_ch_2 = (pip_sub_ch_2>=7) ? 0 : pip_sub_ch_2+1;
								
				for(i = 0; i<2; i++)
				{
					if(pip_sub_ch_2== pip_full_ch)
						pip_sub_ch_2 = PIP_Channel_Select_PIP_2(pip_sub_ch_2);
					if(pip_sub_ch_2== pip_sub_ch_1)
						pip_sub_ch_2 = PIP_Channel_Select_PIP_2(pip_sub_ch_2);					
				}

				buff = I2C_Data_Read(rom_p0, 0xfb);	//... PIP 1 Channel Information [Full : Small]
				buff &= 0xf0;
				buff |= pip_sub_ch_2;

				I2C_Data_Write(rom_p0, 0xfb, buff);	//... PIP 1 Channel Information [Full : Small]
				//Check_Path_Set(pip_sub_ch_2);
				channel_path_Check |= (mask_bit <<pip_sub_ch_2);
				Display_PIP_Initial(pip_sub_ch_2);
				PIP_Title_display();
			}
		}
		else if(channel_select_cursor == 4)
		{
			if((key_code == LEFT_KEY)||(key_code == RIGHT_KEY))
			{			
				temp = (I2C_Data_Read(rom_p0, 0x08) & 0xf0) >>4;		//... Display Setup "PIP1 Mode"		1: Right Bottom  2: Left Bottom 3: Left Top 4: Right Top				
				Pip_Osd_Display(temp, OSD_NOR, 0, 0);
				
				if(key_code == LEFT_KEY)
					temp = (temp<=1) ? 4 : temp -1;
				else
					temp = (temp>=4) ? 1 : temp +1;

				buff = I2C_Data_Read(rom_p0, 0x08) &0x0f;		//... Display Setup "PIP2 Mode"		1: Right Bottom  2: Left Bottom 3: Left Top 4: Right Top
				if(key_code == LEFT_KEY)
				{
					if(temp == buff)
					{
						temp--;
						if(temp < 1)
							temp = 4;
					}
				}else
				{
					if(temp == buff)
					{
						temp++;
						if(temp > 4)
							temp = 1;
					}
				}	

				I2C_Data_Write(rom_p0, 0x08, (temp <<4)|buff);
				Pip_Channel_Pos_Setup(pip_sub_ch_1, temp);
				PIP_Title_display();
			}
		}
		else if(channel_select_cursor == 5)
		{
			if((key_code == LEFT_KEY)||(key_code == RIGHT_KEY))
			{			
				temp = I2C_Data_Read(rom_p0, 0x08) &0x0f; 	//... Display Setup "PIP2 Mode"		1: Right Bottom  2: Left Bottom 3: Left Top 4: Right Top
				Pip_Osd_Display(temp, OSD_NOR, 0, 0);
				
				if(key_code == LEFT_KEY)
					temp = (temp<=1) ? 4 : temp-1;
				else
					temp = (temp>=4) ? 1 : temp+1;

				buff = (I2C_Data_Read(rom_p0, 0x08) &0xf0) >>4;		//... Display Setup "PIP1 Mode"		1: Right Bottom  2: Left Bottom 3: Left Top 4: Right Top
				if(key_code == LEFT_KEY)
				{
					if(temp == buff)
					{
						temp--;
						if(temp < 1)
							temp = 4;
					}
				}else
				{
					if(temp == buff)
					{
						temp++;
						if(temp > 4)
							temp = 1;
					}
				}	

				I2C_Data_Write(rom_p0, 0x08, (buff <<4)|temp);
				Pip_Channel_Pos_Setup(pip_sub_ch_2, temp);
				PIP_Title_display();
			}
		}
	}
	key_code = 0;
	key_sound = ON;	

}

//==================================================================================
//
//
//
//
//
//==================================================================================
void Display_Quad_Function(U8 ch1, U8 ch2, U8 ch3, U8 ch4)
{
	U8 i;

	for(i = 0; i <8; i++)
	{
		Register_Setup(i, 0x03, 0x80, 0);
		Register_Setup(i, 0xba, 0x40, 1);
	}

	Display_Quad(ch1, 1);
	Display_Quad(ch2, 2);
	Display_Quad(ch3, 3);
	Display_Quad(ch4, 4);

	for(i =0; i<8; i++)
	{
		if((i !=ch1)&&(i !=ch2)&&(i !=ch3)&&(i !=ch4))
			Register_Setup(i, 0x03, 0x00, 0);
		Register_Setup(i, 0xba, 0x00, 1);
	}
	
//	i = I2C_Data_Read(rom_p0, 0x03);		//... Display Setup "Title"			1: ON  0: OFF
	if((title_display== ON) && (vcr_mode_in == OFF))
		Camera_Title_Display(); 
}

//==================================================================================
//
//
//
//
//
//==================================================================================
void Display_PIP2_Function()
{
	Display_Full(pip_full_ch);                    
	Display_PIP_Initial(pip_sub_ch_1);
	if(display_mode == PIP2_MODE)
		Display_PIP_Initial(pip_sub_ch_2);
	
//	page_change_flag = ON;	
//	motion_skip_count = 0;	
}

//==================================================================================
//
//
//
//
//
//==================================================================================
U8 PIP_Channel_Select_PIP_2(S8 pip_sel)
{
	if(key_code == LEFT_KEY)
	{
		pip_sel--;
		if(pip_sel < 0)
			pip_sel = 7;
	}
	else
	{
		pip_sel++;
		if(pip_sel > 7)
			pip_sel = 0;
	}
	return pip_sel;
}

//==================================================================================
//
//
//
//
//
//==================================================================================
//... Splite 2 Display
void Display_Splite_2()
{
	U8 buff;
	U8 mask_bit = 0x01;

	if(key_code == ENTER_KEY)
	{
		ch_control_on_off ^= 1;
		if(ch_control_on_off == ON)
		{
			channel_select_cursor = 1;
			splite_2_auto_move = OFF;
		}else
		{
			channel_select_cursor = 0;
			splite_2_auto_move = ON;
		}
		Splite_2_Title_Display();
	}

	if(ch_control_on_off == ON)
	{
		if(key_code == DOWN_KEY)
		{
			channel_select_cursor = (channel_select_cursor>=4) ? 1 : channel_select_cursor+1;
			Splite_2_Title_Display();                       
		}else if(key_code == UP_KEY)
		{
			channel_select_cursor = (channel_select_cursor<=1) ? 4 : channel_select_cursor-1;
			Splite_2_Title_Display();                      
		}

		if(channel_select_cursor == 1)
		{
			if((key_code == LEFT_KEY)||(key_code == RIGHT_KEY))
			{
				Register_Setup(pip_full_ch, 0x3f, 0x00, 0);
				channel_path_Check &= ~(mask_bit <<pip_full_ch);
				if(key_code == LEFT_KEY)
					pip_full_ch = (pip_full_ch<=0) ? 7: pip_full_ch-1;
				else
					pip_full_ch = (pip_full_ch>=7) ? 0 : pip_full_ch+1;
				
				if(pip_full_ch== pip_sub_ch_1)
					pip_full_ch = PIP_Channel_Select_PIP_2(pip_full_ch);

				buff = I2C_Data_Read(rom_p0, 0xfc);				//... PIP 1 Channel Information [Full : Small]
				buff &= 0x0f;
				buff |= pip_full_ch << 4;
				I2C_Data_Write(rom_p0, 0xfc, buff);				//... Splite 2 Channel Information [Full : Small]
				
//				Display_splite_Common();
				//Check_Path_Set(pip_full_ch);
				channel_path_Check |= (mask_bit <<pip_full_ch);
				Display_2_splite(pip_full_ch);
				Splite_2_Title_Display();
			}
		}
		else if(channel_select_cursor == 2)
		{
			if((key_code == LEFT_KEY)||(key_code == RIGHT_KEY))
			{	
				buff=(key_code == LEFT_KEY)?0:1;
				Channel_Control_Splite_2_Move(pip_full_ch, buff, 3);
			}
		}
		else if(channel_select_cursor == 3)
		{
			if((key_code == LEFT_KEY)||(key_code == RIGHT_KEY))
			{			
				Register_Setup(pip_sub_ch_1, 0x3f, 0x00, 0);
				channel_path_Check &= ~(mask_bit <<pip_sub_ch_1);
				if(key_code == LEFT_KEY)
					pip_sub_ch_1 = (pip_sub_ch_1<=0) ? 7: pip_sub_ch_1-1;
				else
					pip_sub_ch_1 = (pip_sub_ch_1>=7) ? 0 : pip_sub_ch_1+1;
				
				if(pip_sub_ch_1== pip_full_ch)
					pip_sub_ch_1 = PIP_Channel_Select_PIP_2(pip_sub_ch_1);
				
				buff = I2C_Data_Read(rom_p0, 0xfc);			//... PIP 1 Channel Information [Full : Small]
				buff &= 0xf0;
				buff |= pip_sub_ch_1;
				I2C_Data_Write(rom_p0, 0xfc, buff);			//... PIP 1 Channel Information [Full : Small]

//				Display_splite_Common();
				//Check_Path_Set(pip_sub_ch_1);
				channel_path_Check |= (mask_bit <<pip_sub_ch_1);
				Display_2_splite(pip_sub_ch_1);
				Splite_2_Title_Display();
			}
		}
		else if(channel_select_cursor == 4)
		{
			if((key_code == LEFT_KEY)||(key_code == RIGHT_KEY))
			{	
				buff=(key_code == LEFT_KEY)?0:1;
				Channel_Control_Splite_2_Move(pip_sub_ch_1, buff, 3);
			}
		}
	}
	key_code = 0;
	key_sound = ON;
	
//	Splite_Auto_Move_Function();
}

//==================================================================================
//
//
//
//
//
//==================================================================================
void Display_Splite_2_Function()
{
	U8 i;

	for(i = 0; i <8; i++){
		Register_Setup(i, 0x03, 0x80, 0);
		Register_Setup(i, 0xba, 0x40, 1);
	}	
	Display_2_splite(pip_full_ch);
	Display_2_splite(pip_sub_ch_1);
	for(i=0; i<8; i++)
	{
		if((i !=pip_full_ch)&&(i !=pip_sub_ch_1))
			Register_Setup(i, 0x00, i%4, 0);
		Register_Setup(i, 0xba, 0x00, 1);
	}
}

//==================================================================================
//function	:	Channel_Control_Splite_2_Move
//input :
//	chip_select	:
//	channel_select:
//	inc_dec		:
//	move_speed	:
//output 	: 	null
//==================================================================================
void Channel_Control_Splite_2_Move(U8 ch_num, U8 inc_dec, U8 move_speed)
{
	U8 buff, m_s;
	S16 rom_data;

	if(ch_num<4)
		m_s =MASTER;
	else
		m_s = SLAVE1;
	rom_data = ReadAsicByte(m_s, DVC_PG0,(((ch_num % 4)* 0x10)+0x06));
	rom_data &= 0x0003;
	rom_data <<= 8;
	rom_data |= ReadAsicByte(m_s, DVC_PG0,(((ch_num % 4)* 0x10)+0x02));
	
	if(inc_dec == 1)
	{
		rom_data += move_speed;
		if(rom_data > 0x017f)
			rom_data = 0x017f;
	}
	else
	{
		rom_data -= move_speed;
		if(rom_data < 0x0022)
			rom_data = 0x0022;
	}

/*	if(splite_2_auto_move == OFF)
	{
		if(channel_select_cursor == 2)
		{
			if(pip_full_ch > 4)
				buff = 0x8c;
			else
				buff = 0x84;
		}
		else if(channel_select_cursor == 4)
		{
			if(pip_sub_ch_1 > 4)
				buff = 0x8c;
			else
				buff = 0x84;
		}*/
/*
		rom_address = (buff + ((channel_sel-1)* 2));
		temp = (rom_data & 0x0300) >> 8;
		I2C_Data_Write(rom_p0, rom_address, (temp|0x28));
		temp = rom_data & 0x00ff;
		I2C_Data_Write(rom_p0, (rom_address+1), temp);*/
//	}	

	buff = (rom_data & 0x0300) >> 8;
	WriteAsicByte(m_s, DVC_PG0,(((ch_num % 4)* 0x10)+0x06), buff|(b_video_fmt == NTSC ? 0x08 : 0x28));//temp|0x88,   if set 0x88 to page0 0x06, video will be lost
	buff = rom_data & 0x00ff;
	WriteAsicByte(m_s, DVC_PG0,(((ch_num % 4)* 0x10)+0x02),buff);		
}

//==================================================================================
//
//
//
//
//
//==================================================================================
void Splite_Auto_Move_Function()
{
	U8 buff, N_P;
	S16 rom_data;
	
	if(splite_2_auto_move == ON)
	{
//		if((splite_2_auto_stop == OFF) || (splite_2_flag == ON))
//		{		
			N_P	= (b_video_fmt == PAL) ? 12 : 0;	
//			WriteAsicByte(MASTER, DVC_PG2, 0x2b, 40);
			SetBox(2, 34, 18, 20+N_P, 15);
			SetBoxPlaneSel(2, 3, 6);
			SetBoxMix(PTH_X,2,ON);		        //... set box mix
			SetBoxBnd(PTH_X,2,ON);			 //... set box boundary
			SetBoxPln(PTH_X,2,ON);			//... set box plane area
//			buff = ReadAsicByte(MASTER, DVC_PG2, 0x2b);
//			WriteAsicByte(MASTER, DVC_PG2, 0x2b, buff|0xc0);

			SetBox(3, 298, 18, 20+N_P, 15);	
			SetBoxPlaneSel(2, 3, 6);
			SetBoxMix(PTH_X,3,ON);		        //... set box mix
			SetBoxBnd(PTH_X,3,ON);			 //... set box boundary
			SetBoxPln(PTH_X,3,ON);			//... set box plane area
			rom_data = ReadAsicByte((pip_full_ch <4) ?MASTER : SLAVE1, DVC_PG0,(((pip_full_ch % 4)* 0x10)+0x06));
			rom_data &= 0x0003;
			rom_data <<= 8;
			rom_data |= ReadAsicByte((pip_full_ch <4) ?MASTER : SLAVE1, DVC_PG0,(((pip_full_ch % 4)* 0x10)+0x02));

//			if((splite_2_auto_stop == OFF) && (splite_2_flag == OFF))
//			{
				if(rom_data > 0x017e)
					splite_2_auto_flag_L =  0;
		
				else if(rom_data < 0x0023)
					splite_2_auto_flag_L =  1;
				Channel_Control_Splite_2_Move(pip_full_ch, splite_2_auto_flag_L, 1);
//			}

			buff = (rom_data - 0x22)/0x1d;
			buff += 34;
			SetBoxPlaneSel(0, 0, 0);
			SetBoxMix(PTH_X,0,ON);		        //... set box mix
			SetBoxBnd(PTH_X,0,ON);			 //... set box boundary
			SetBoxPln(PTH_X,0,ON);			//... set box plane area
			SetBox(0, buff, 6, 21+N_P, 13);
			rom_data = ReadAsicByte((pip_sub_ch_1 <4) ?MASTER : SLAVE1, DVC_PG0,(((pip_sub_ch_1 % 4)* 0x10)+0x06));
			rom_data &= 0x0003;
			rom_data <<= 8;
			rom_data |= ReadAsicByte((pip_sub_ch_1 <4) ?MASTER : SLAVE1, DVC_PG0,(((pip_sub_ch_1 % 4)* 0x10)+0x02));
//			if((splite_2_auto_stop == OFF) && (splite_2_flag == OFF))
//			{		
				if(rom_data > 0x017e)
					splite_2_auto_flag_R =  0;
				
				else if(rom_data < 0x0023)
					splite_2_auto_flag_R =  1;
				
				Channel_Control_Splite_2_Move(pip_sub_ch_1, splite_2_auto_flag_R, 1);
//			}

			buff = (rom_data - 0x22)/0x1d;
			buff += 34;
			SetBoxPlaneSel(1, 1, 0);
			SetBoxMix(PTH_X,1,ON);		        //... set box mix
			SetBoxBnd(PTH_X,1,ON);			 //... set box boundary	
			SetBoxPln(PTH_X,1,ON);			//... set box plane area
			SetBox(1, (buff+265), 6, 21+N_P, 13);
//			Set_Box_Bnd(D_PATH, 0, 1, BOX_OBND);		
//			Set_Box_Bnd(D_PATH, 2, 3, BOX_OBND);

//			sub_menu_select_bar--;
//			if(sub_menu_select_bar == 0)
//				splite_2_auto_stop = ON;
//		}
//		else if(splite_2_auto_stop == ON) 
//		{
//			splite_2_auto_move = OFF;
//			splite_2_flag = OFF;				
//		}
	}
}

//==================================================================================
//
//
//
//
//
//==================================================================================
/*void Splite_Move_OSD_Display()
{
	U8 N_P;
	U8 i, temp;
	
	if(b_video_fmt == PAL) N_P = 10;
	else N_P = 11;
			
	if(splite_2_flag == ON)
	{
		if(channel_select_cursor == 2)
			temp = OSD_BAR;
		else
			temp = OSD_NOR;
		
		WriteOSDFnt(PTH_Y,MASTER,11,N_P,temp,UP_DOWN_FONT);
		WriteOSDFnt(PTH_Y,MASTER,12,N_P,temp,(UP_DOWN_FONT+1));
		WriteOSDFnt(PTH_Y,MASTER,13,N_P,temp,(UP_DOWN_FONT+2));
		WriteOSDFnt(PTH_Y,MASTER,7,N_P,temp,LEFT_ARROW_FONT);
		WriteOSDFnt(PTH_Y,MASTER,17,N_P,temp,RIGHT_ARROW_FONT);
		WriteOSDFnt(PTH_Y,MASTER,10,N_P,temp,OSD_BAR_FONT_R);
		WriteOSDFnt(PTH_Y,MASTER,14,N_P,temp,OSD_BAR_FONT_L);
		for(i=8;i<10;i++)
			WriteOSDFnt(PTH_Y,MASTER,i,N_P,temp,OSD_BAR_FONT_C);
					
		for(i=15;i<17;i++)
			WriteOSDFnt(PTH_Y,MASTER,i,N_P,temp,OSD_BAR_FONT_C);

		if(channel_select_cursor == 4)
			temp = OSD_BAR;
		else
			temp = OSD_NOR;

		WriteOSDFnt(PTH_Y,MASTER,31,N_P,temp,LF_RG_FONT);
		WriteOSDFnt(PTH_Y,MASTER,32,N_P,temp,(LF_RG_FONT+1));
		WriteOSDFnt(PTH_Y,MASTER,33,N_P,temp,(LF_RG_FONT+2));			
		WriteOSDFnt(PTH_Y,MASTER,27,N_P,temp,LEFT_ARROW_FONT);
		WriteOSDFnt(PTH_Y,MASTER,37,N_P,temp,RIGHT_ARROW_FONT);			
		WriteOSDFnt(PTH_Y,MASTER,30,N_P,temp,OSD_BAR_FONT_R);
		WriteOSDFnt(PTH_Y,MASTER,34,N_P,temp,OSD_BAR_FONT_L);			
		for(i=28;i<30;i++)
			WriteOSDFnt(PTH_Y,MASTER,i,N_P,temp,OSD_BAR_FONT_C);

		for(i=35;i<37;i++)
			WriteOSDFnt(PTH_Y,MASTER,i,N_P,temp,OSD_BAR_FONT_C);
	}
	else
	{
		for(i=5;i<40;i++)
			WriteOSDFnt(PTH_Y,MASTER,i,N_P,OSD_NOR,BLANK_FONT);
	}
}*/
//==================================================================================
//
//
//
//
//
//==================================================================================
void Auto_Sequence_Ch_Change_Function()
{
	U8 i;
	
	RemoveOSDFnt(0, 22, 89, 287);			//Clear OSD
	WriteOSDStr(PTH_Y,MASTER, 454, 9, OSD_NOR, "AUTO");
	if((display_mode == QUAD_A) ||(display_mode == QUAD_B))
	{
		switch(display_mode)
		{
			case QUAD_A :
						pip_full_ch = 0;
						pip_sub_ch_1 = 1;
						pip_sub_ch_2 = 2;
						pip_sub_ch_3 = 3;
						Display_Quad_Function(0, 1, 2, 3);
						break;
			case QUAD_B :
						pip_full_ch = 4;
						pip_sub_ch_1 = 5;
						pip_sub_ch_2 = 6;
						pip_sub_ch_3 = 7;
						Display_Quad_Function(4, 5, 6, 7);
		}		
	}	
	else
	{
		i =display_mode -(2 +(display_mode-2) /5);	//display channel
		Display_Full(i);	
		channel_path_Check = 0x01;
		channel_path_Check <<=i;
	}	
}

//==================================================================================
//
//
//
//
//
//==================================================================================
void Auto_Sequence_Function_ON_OFF()
{	
	led_on_off_flag = key_code;
	key_sound = ON;
	Event_Reset();
	RemoveBox();
	freeze_on = OFF;
	Channel_Freeze_Function();
	RemoveOSDFnt(0, 22, 89, 287);			//Clear OSD
	WriteOSDStr(PTH_Y,MASTER, 454, 9, OSD_NOR, "AUTO");
	auto_sequence_on = ON;
	auto_mode_flag =ON;
	auto_seq_option = ON;
	one_sec = 0;
		
	display_mode = QUAD_A;
	display_mode_backup = display_mode;
}

//==================================================================================
//
//
//
//
//
//==================================================================================
void Auto_Sequence_Function() 
{
	U8 sequence_dwell_time;
	U8 mask_bit = 0x01;
	U8 skip_flag = 0;

	sequence_dwell_time = I2C_Data_Read(rom_p0, display_mode + 0x68);		//... Auto Sequence Dwell Time
	if(auto_seq_option == ON)
	{		
		if(display_mode == QUAD_A)
			if(((loss_channel_info_backup & 0x0f) != 0x0f) && (sequence_dwell_time != 0))
			{
				channel_path_Check = 0x0f;
				Auto_Sequence_Ch_Change_Function();
			}
			else 
				skip_flag = 1;
		else if(display_mode == QUAD_B)
			if(((loss_channel_info_backup & 0xf0) != 0xf0) && (sequence_dwell_time != 0))
			{
				channel_path_Check = 0xf0;
				Auto_Sequence_Ch_Change_Function();
			}
			else
				skip_flag = 1;
		else if(display_mode >QUAD_A && display_mode <QUAD_B)
			if((loss_channel_info_backup & (mask_bit <<(display_mode -2))) == 0 && (sequence_dwell_time != 0))
				Auto_Sequence_Ch_Change_Function();
			else
				skip_flag = 1;
		else if(display_mode >QUAD_B && display_mode <11)
			if((loss_channel_info_backup & (mask_bit <<(display_mode -3))) == 0 && (sequence_dwell_time != 0))
				Auto_Sequence_Ch_Change_Function();
			else
				skip_flag = 1;
		auto_seq_option = OFF;
	}
	
	if((skip_flag != 0) ||(one_sec > sequence_dwell_time))
	{
		one_sec = 0;	
		auto_seq_option = ON;		
		display_mode_backup = display_mode;
		display_mode++;
		
		if(display_mode > CH8_FULL)
			display_mode = QUAD_A;
	}	
}

//==================================================================================
//
//
//
//
//
//==================================================================================
void Zoom_Function_ON_OFF()
{
	U8 N_P = 0;
	
//	if(key_code ==  ZOOM_KEY)
//	{
		led_on_off_flag = key_code;
		key_sound = ON;
		Event_Reset();
		RemoveBox();

/*		if(b_video_fmt == NTSC)
			N_P = 2;
		WriteOSDClr(PTH_Y,MASTER,11, 13-N_P, 3, 5); 	//Clear Quad mode OSD
		WriteOSDClr(PTH_Y,MASTER,32, 13-N_P, 3, 5); 	//Clear Quad mode OSD

		if(b_video_fmt == NTSC)
			N_P = 4;
		WriteOSDClr(PTH_Y,MASTER,22, 25-N_P, 3, 2); 	//Clear Full mode
		WriteOSDClr(PTH_Y,MASTER,11, 24-N_P, 3, 2);	//Clear Splite2 mode
		WriteOSDClr(PTH_Y,MASTER,32, 24-N_P, 3, 2);	//Clear Splite2 mode
		Clr_realtime8_osd();	
		Clr_pseudo16_osd();
*/		
		RemoveOSDFnt(0, 22, 89, 287);	//Clear OSD
		WriteOSDClr(PTH_Y, MASTER, 454, 9, 8, 12);
		if(auto_mode_flag ==ON)
		{
			auto_sequence_on ^=1;
			one_sec = 0;
		}
		if(display_mode == SPLITE_2)
			splite_2_auto_move ^=1;
		zoom_mode_in ^= 1;
		zoom_mode_on =OFF;
		if(zoom_mode_in == ON)
		{
			zoom_x_loc = 0x2d;
			if(b_video_fmt == NTSC)
				zoom_y_loc = 0x1e;
			else
				zoom_y_loc = 0x24;
			WriteAsicByte(MASTER,DVC_PG1,0x0d, zoom_x_loc);
			WriteAsicByte(MASTER,DVC_PG1,0x0e, zoom_y_loc);
			WriteAsicByte(ALL_CHIP, DVC_PG1, 0x0c, 0x2f);
		}
		else
		{
			WriteAsicByte(ALL_CHIP, DVC_PG1,0x0c,0x00);
//			buff = I2C_Data_Read(rom_p0, 0x03);				//... Display Setup "Title"			1: ON  0: OFF
			if(title_display== ON)
				Camera_Title_Display();
//			auto_sequence_on = OFF;
		}
		
//	}
}

//==================================================================================
//
//
//
//
//
//==================================================================================
void Zoom_Function()
{	
	switch(key_code) {
		case RIGHT_KEY : key_code = 0;
						key_sound = ON;
						zoom_x_loc++;
						if(zoom_x_loc > 0x5a)
							zoom_x_loc = 0x5a;
						break;
		case LEFT_KEY : 	key_code = 0;
						key_sound = ON;
						zoom_x_loc--;
						if(zoom_x_loc < 0x00)
							zoom_x_loc = 0x00;
						break;
		case DOWN_KEY : key_code = 0;
						key_sound = ON;
						zoom_y_loc++;
						if(b_video_fmt == NTSC)
						{
							if(zoom_y_loc > 0x3c)
								zoom_y_loc = 0x3c;
						}
						else
						{
							if(zoom_y_loc > 0x48)
								zoom_y_loc = 0x48;
						}
						break;
		case UP_KEY : 	key_code = 0;
						key_sound = ON;
						zoom_y_loc--;
						if(zoom_y_loc < 0x00)
							zoom_y_loc = 0x00;
						break;
		case ENTER_KEY : key_code = 0;
						key_sound = ON;
						zoom_mode_on ^= 1;
						if(zoom_mode_on == ON)
							WriteAsicByte(ALL_CHIP, DVC_PG1,0x0c,0xaf);
						else
							WriteAsicByte(ALL_CHIP, DVC_PG1,0x0c,0x2f);
						break;							
			}		
	WriteAsicByte(ALL_CHIP,DVC_PG1,0x0d,zoom_x_loc);
	WriteAsicByte(ALL_CHIP,DVC_PG1,0x0e,zoom_y_loc);
}
void Zoom_Function_Test(U8 x_pos, U8 y_pos, U8 on_off)
{	
	if(x_pos<0)
		x_pos = 0;
	else if(x_pos>90)
		x_pos = 90;
	if(y_pos<0)
		y_pos = 0;
	else if(y_pos>((b_video_fmt == NTSC)?60:72))
		y_pos = (b_video_fmt == NTSC)?60:72;

	if(on_off == ON)
	{
		WriteAsicByte(ALL_CHIP,DVC_PG1,0x0d,x_pos);
		WriteAsicByte(ALL_CHIP,DVC_PG1,0x0e,y_pos);

		WriteAsicByte(ALL_CHIP, DVC_PG1,0x0c,0xaf);
	}
	else
	{
		WriteAsicByte(ALL_CHIP, DVC_PG1,0x0c,0x2f);							
	}
}
//==================================================================================
//
//
//
//
//
//==================================================================================
void Border_Line_Setup_Initial()
{
	U8 i, temp, buff;
	
	temp = I2C_Data_Read(rom_p0, 0x06)&0x0f;		//... Display Setup "Border Color"	1: Black  2: 25% Gray 3: 75% Gray 4: White 5: OFF

	if(temp <5)
	{
		for(i = 0; i <8; i++)                                      //Enable channel boundary for X and Y 
			Register_Setup(i, 0xfd, 0x02, 1);

		temp--;
		temp <<= 4;		
		buff = ReadAsicByte(MASTER, DVC_PG1, 0x0f);
		buff &= 0xcf;
		buff |= temp;
		WriteAsicByte(MASTER, DVC_PG1, 0x0f, buff);  		//Set BNDCOL for X path 
		
		buff = ReadAsicByte(SLAVE1,DVC_PG1, 0x0f);
		buff &= 0xcf;
		buff |= temp;
		WriteAsicByte(SLAVE1, DVC_PG1, 0x0f, buff);  		//Set BNDCOL for X path
	}
	else
	{
		for(i = 0; i < 8; i++)
			Register_Setup(i, 0xfd, 0x00, 1);
	}

	i = I2C_Data_Read(rom_p0, 0x87);					//... Screen Position X
	temp = ReadAsicByte(MASTER, DVC_PG1, 0xa8);
	temp &= 0xc0;
	temp |= (i <<1);
	WriteAsicByte(ALL_CHIP, DVC_PG1, 0xa8, temp);		//Set ACTIVE_HDEL
	
	i = I2C_Data_Read(rom_p0, 0x88);					//... Screen Position Y
	temp = ReadAsicByte(MASTER, DVC_PG1, 0xa7);
	temp &= 0xe0;
	temp |= (i <<2);
	WriteAsicByte(ALL_CHIP, DVC_PG1, 0xa7, temp);		//Set ACTIVE_VDEL
}

//==================================================================================
//
//
//
//
//
//==================================================================================
void Camera_Setup_Initial()
{
	U8 i, read_data;
	U8 j, buff, temp, chip_select;

	for(i =0; i <8; i++)
	{
		read_data = I2C_Data_Read(rom_p0, 0x0e +i*6)&0x30;			//... Camera Mirroring Setup		1: ON  0: OFF
		Register_Setup(i, 0xcf, read_data, 1);
	}

	chip_select = MASTER;
	temp = 0x09;
	
	for(i = 0; i < 2; i++)
	{
		for(j = 0; j < 4; j++)
	    	{
			read_data = I2C_Data_Read(rom_p0, temp + j * 6);					//... Camera Brightness Setup
			WriteAsicByte(chip_select, DVC_PG0, 0x0a + j * 0x10, read_data);          	//The (0x80) is used Just for test (070823)
		
			read_data = I2C_Data_Read(rom_p0, temp +1 + j * 6);					//... Camera Contrast Setup
			WriteAsicByte(chip_select,  DVC_PG0, 0x09 + j * 0x10, read_data);         	//The (0x80) is used Just for test (070823)
		
			read_data = I2C_Data_Read(rom_p0, temp +2 + j * 6);					//... Camera Saturation Setup
			WriteAsicByte(chip_select, DVC_PG0, 0x08 + j * 0x10, read_data);
		
			read_data = I2C_Data_Read(rom_p0, temp +3 + j * 6);					//... Camera Hue Setup
			WriteAsicByte(chip_select, DVC_PG0, 0x07 + j * 0x10, read_data);
		
			read_data = I2C_Data_Read(rom_p0, temp +4 + j * 6);					//... Camera Sharpness Setup		1: Low  2: Middle 3: High 4: Highest	
			buff = ReadAsicByte(chip_select, DVC_PG0, 0x0b + j * 0x10);
			buff &=0xf0;
			WriteAsicByte(chip_select, DVC_PG0, 0x0b + j * 0x10, buff|(read_data-1));          	//Set YPEAK 
	   	}
		chip_select = SLAVE1;
		temp = 0x21;
	}
}
#if 0
//==================================================================================
//
//
//
//
//
//==================================================================================
void Motion_Setup_Initial()
{
	U8 i, j, k, buff,chip_select;
	U8 read_data,mask_bit =0x01;

	for(i = 0;i <8; i++)
	{
		read_data = I2C_Data_Read(rom_p0, (0x89 + (i * 7)));		//... Motion Detect On/Off		
		switch(read_data) {
			case 1 : motion_enable_flag &= ((mask_bit << i) ^ 0xff);
				      break;
			case 2 : motion_enable_flag |= (mask_bit << i);
				      break;
			}
	}

	chip_select = MASTER;	
	for(i = 0; i < 2; i++)
	{	
		for(j = 0; j < 4; j++)
			for(k = 0; k < 24; k += 2)
			{		
				buff = j * 24 + k +i *0x60;						//... Read Address			
				read_data = I2C_Data_Read(rom_p1, buff);								//... Motion Mask Setup_Low			
				WriteAsicByte(chip_select, DVC_PG2, 0x87 + k + j * 0x20, read_data);        	//Set MD_MASK[7..0] for tw2835 (070823)			
				read_data = I2C_Data_Read(rom_p1, buff+1);							//... Motion Mask Setup_High
				WriteAsicByte(chip_select, DVC_PG2, 0x86 + k + j * 0x20, read_data);              //Set MD_MASK[15..8] for tw2835 (070823)

			}
		chip_select = SLAVE1;
	}

	chip_select = MASTER;
	for(i = 0; i <2; i++)
	{
		for(j =0; j<4; j++)
		{	
			read_data = I2C_Data_Read(rom_p0, 0x8e + (j +i*4)*7);				//... Motion Sensitivity, Velocity
			buff = 16 - read_data;
			WriteAsicByte(chip_select, DVC_PG2, j * 0x20 + 0x83, buff|0x40);
		
			read_data = I2C_Data_Read(rom_p0, 0x8f + (j +i*4)*7);				//... Motion Sensitivity, Velocity
//			buff = read_data;
//		buff &= 0x3f;                                                                       
//			if(buff > 8)
//				buff *= 2;		
			WriteAsicByte(chip_select, DVC_PG2, j * 0x20 + 0x84, read_data);           //Set MD_SPEED 
		}
		chip_select = SLAVE1;
	}
	
}
#endif
//==================================================================================
//
//
//
//
//
//==================================================================================
void Event_Reset()
{
//	osd_alarm_channel_info = 0;
	osd_motion_channel_info = 0;
	
	Write_I2C_1Byte(rtc_sadd,0x09,100);			//... Buzzer Hold Time Counter Write
	event_on = OFF;
	alarm_on = OFF;
	RELAY_OUT = 1;
	BUZZER_OUT = 1;
}

//==================================================================================
/*void RS232_TX_Data_Setup()
{
	U8 i, mask_bit;
	U8 temp;

	for(i=0;i<8;i++)
	{
		mask_bit = 0x01;
		if(i<4)
		{
			if((osd_alarm_channel_info & (mask_bit << i)) != 0)
				serial_tdata[i+2] |= 0x10;
			else
				serial_tdata[i+2] &= 0xef;					
		}
		else
		{
			if((osd_alarm_channel_info & (mask_bit << i)) != 0)
				serial_tdata[i-2] |= 0x01;
			else
				serial_tdata[i-2] &= 0xfe;
		}
	}

	if((vcr_mode_in == ON) || (menu_on == ON))
	{
		for(i=0;i<4;i++)
		{
			serial_tdata[i+2] &= 0x00;
			serial_tdata[i+2] |= 0x88;
		}
	}
	else if(display_mode == PSEUDO_8CH)
	{
		for(i=0;i<8;i++)
		{
			mask_bit = 0x01;
			if(i<4)
			{
				if((loss_channel_info & (mask_bit << i)) != 0)
				{
					serial_tdata[i+2] &= 0x1f;
					serial_tdata[i+2] |= 0x60;
				}
				else
				{
					serial_tdata[i+2] &= 0x1f;
					serial_tdata[i+2] |= 0x40;
				}
			}
			else
			{
				if((loss_channel_info & (mask_bit << i)) != 0)
				{
					serial_tdata[i-2] &= 0xf1;
					serial_tdata[i-2] |= 0x06;
				}
				else
				{
					serial_tdata[i-2] &= 0xf1;
					serial_tdata[i-2] |= 0x04;
				}
			}
		}
	}
	else
	{
		for(i=0;i<8;i++)
		{
			mask_bit = 0x01;
			if(i<4)
			{
				temp = ReadAsicByte(MASTER,DVC_PG0, (0x0d+(i*0x10)));
				if((temp & 0x08) == 0)
				{
					if((loss_channel_info & (mask_bit << i)) != 0)
					{
						serial_tdata[i+2] &= 0x1f;
						serial_tdata[i+2] |= 0x60;
					}
					else
					{
						serial_tdata[i+2] &= 0x1f;
						if((osd_motion_channel_info & (mask_bit << i)) != 0)
							serial_tdata[i+2] |= 0x20;
					}
				}
				else
				{
					serial_tdata[i+2] &= 0x1f;
					serial_tdata[i+2] |= 0x80;
				}
			}
			else
			{
				temp = ReadAsicByte(MASTER,DVC_PG0, (0x0d+((i-4)*0x10)));
				if((temp & 0x08) != 0)
				{
					if((loss_channel_info & (mask_bit << i)) != 0)
					{
						serial_tdata[i-2] &= 0xf1;
						serial_tdata[i-2] |= 0x06;
					}
					else
					{
						serial_tdata[i-2] &= 0xf1;
						if((osd_motion_channel_info & (mask_bit << i)) != 0)
							serial_tdata[i-2] |= 0x02;
					}
				}
				else
				{
					serial_tdata[i-2] &= 0xf1;
					serial_tdata[i-2] |= 0x08;
				}
			}
		}
	}

	serial_tdata[0] = 0xe0;
	if(ch4_ch8_option == CH_4_MODE)
		serial_tdata[1] = 0x40;
	else
		serial_tdata[1] = 0x80;
	
	if(menu_on == ON)
		serial_tdata[0] = 0xc8;			//... Menu ON
	else
	{
		serial_tdata[0] &= 0xf7;			//... Menu OFF
		
		if(freeze_on == ON)				//... Freeze ON
			serial_tdata[0] |= 0xc2;
		else
			serial_tdata[0] &= 0xfd;
		if((zoom_mode_in == ON) || (vcr_zoom_on == ON))			//... Zoom ON
			serial_tdata[0] |= 0xc4;
		else
			serial_tdata[0] &= 0xfb;

		if(vcr_mode_in == ON)				//... PB ON
			serial_tdata[0] |= 0xc1;
		else
			serial_tdata[0] &= 0xfe;

		if(display_mode != PSEUDO_8CH)
		{
			temp = ReadAsicByte(MASTER,DVC_PG0, 0x0d);
			if((temp & 0x08) != 0)
				serial_tdata[1] |= 0x10;
			else
				serial_tdata[1] &= 0xef;
			
			temp = ReadAsicByte(MASTER,DVC_PG0, 0x1d);
			if((temp & 0x08) != 0)
				serial_tdata[1] |= 0x20;
			else
				serial_tdata[1] &= 0xdf;
			
			temp = ReadAsicByte(MASTER,DVC_PG0, 0x2d);
			if((temp & 0x08) != 0)
				serial_tdata[0] |= 0x10;
			else
				serial_tdata[0] &= 0xef;
			
			temp = ReadAsicByte(MASTER,DVC_PG0, 0x3d);
			if((temp & 0x08) != 0)
				serial_tdata[0] |= 0x20;
			else
				serial_tdata[0] &= 0xdf;
		}
		else
		{
			serial_tdata[0] &= 0xcf;
			serial_tdata[1] &= 0xcf;
		}
			

		
		serial_tdata[1] &= 0xf0;

		if(vcr_mode_in == OFF)
		{
			if(auto_sequence_on == ON) 
				serial_tdata[1] |= 0x07;
			else
			{
				if((display_mode == CH1_FULL) || (display_mode == CH5_FULL))
					serial_tdata[1] |= 0x01;
				else if((display_mode == CH2_FULL) || (display_mode == CH6_FULL))
					serial_tdata[1] |= 0x02;
				else if((display_mode == CH3_FULL) || (display_mode == CH7_FULL))
					serial_tdata[1] |= 0x03;
				 else if((display_mode == CH4_FULL) || (display_mode == CH8_FULL))
					serial_tdata[1] |= 0x04;
				 else if(display_mode == QUAD_A) 
					serial_tdata[1] |= 0x05;
				 else if(display_mode == QUAD_B) 
					serial_tdata[1] |= 0x06;			
				 else if(display_mode == PSEUDO_8CH) 
					serial_tdata[1] |= 0x08;
				  else if(display_mode == PIP2_MODE) 
					serial_tdata[1] |= 0x09;
				 else if(display_mode == PIP1_MODE) 
					serial_tdata[1] |= 0x0a;
				 else if(display_mode == SPLITE_2) 
					serial_tdata[1] |= 0x0b;
			}
		}
		else
		{
			if(vcr_zoom_on == ON)
			{
				if(zoom_x_loc != 0)
				{
					if(zoom_y_loc != 0)
						serial_tdata[1] |= 0x04;
					else
						serial_tdata[1] |= 0x02;
				}
				else
				{
					if(zoom_y_loc != 0)
						serial_tdata[1] |= 0x03;
					else
						serial_tdata[1] |= 0x01;
				}
			}
			else
				serial_tdata[1] |= 0x05;
			
		}
	}

	if(serial_tx_flag_start == ON)
	{
		serial_tx_flag_start = OFF;
		
		SBUF = serial_tdata[serial_counter_tx];

		serial_counter_tx++;

		if(serial_counter_tx > 5)
			serial_counter_tx = 0;
	}

}*/
//==================================================================================


/*void change_ch_control_on(void)
{	
	
	ch_control_on_off = ON;
}*/

